var app = getApp();
var util = require('../../../js/common.util.js');
Page({
  data: {
    currentTab: 0, //收入
    UserInfo: {},
    DataList: [],
    page: 1,
    pageSize: 20,
    hasMoreData: true,
  },
  swiperTab: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });


    this.setData({
      page: 1
    })
    this.loadData()
  },
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }

    });
  },
   dealNumber (money) {
    if (money && money != null) {
      money = String(money);
      var left = money.split('.')[0], right = money.split('.')[1];
      right = right ? (right.length >= 2 ? '.' + right.substr(0, 2) : '.' + right + '0') : '.00';
      var temp = left.split('').reverse().join('').match(/(\d{1,3})/g);
      return (Number(money) < 0 ? "-" : "") + temp.join(',').split('').reverse().join('') + right;
    } else if (money === 0) {   //注意===在这里的使用，如果传入的money为0,if中会将其判定为boolean类型，故而要另外做===判断
      return '0.00';
    } else {
      return "";
    }
  },
  onShow: function () {
    let self = this
    //账户信息
    app.request({
      url: 'api/wechat/getUserInfo',
      method: "GET",
      success: function (res) {
        res.data.info.userBalanceText = self.dealNumber(res.data.info.userBalance)
        res.data.info.sumComeInBalance = self.dealNumber(res.data.info.sumComeInBalance)
        res.data.info.sumWithdrawBalance = self.dealNumber(res.data.info.sumWithdrawBalance)
        res.data.info.redAmount = self.dealNumber(res.data.info.redAmount)
        self.setData({
          UserInfo: res.data.info
        })
      }
    })

    this.setData({
      page: 1
    })
    this.loadData()
  },
  //下拉
  onPullDownRefresh: function () {
    this.setData({
      page: 1
    })
    this.loadData()
    wx.stopPullDownRefresh();
  },
  //上拉
  onReachBottom: function () {
    if (this.data.hasMoreData) {
      this.loadData()
    } else {
      wx.showToast({
        title: '没有更多了',
      })
    }
  },
  shenqing(){
    if (this.data.UserInfo.userBalance <= 0){
      wx.showModal({
        title: '提示',
        content: '您当前余额为0.00元，不能申请提现，红包余额将在提成金额累计达到2000.00元后发放。',
        showCancel: false,
      })
      return
    }
    wx.navigateTo({
      url: '/page/UserCenter/withdrawal/index',
    })
  },
  loadData() {
    var transactionType = 1
    if (this.data.currentTab == 0){
      transactionType = 1
    }else{
      transactionType = 2
    }
    let self = this
    app.request({
      url: 'api/wechat/getPageOfTransactionByUserId',
      method: "GET",
      data:{
        transactionType: transactionType,
        page: self.data.page,
        pageSize: self.data.pageSize
      },
      success: function (res) {
        if (res.data != null && res.data.Data.length > 0) {
          var listTemp = self.data.DataList

          if (self.data.page == 1) {
            listTemp = []
          }
          var list = res.data.Data
          for (var i = 0; i < list.length; i++) {
            list[i].transaction.createTime = util.formatTime(list[i].transaction.createTime)
            list[i].transaction.amount = self.dealNumber(list[i].transaction.amount)
          }
          if (list.length < self.data.pageSize) {
            self.setData({
              DataList: listTemp.concat(list),
              hasMoreData: false
            })
          } else {
            self.setData({
              DataList: listTemp.concat(list),
              hasMoreData: true,
              page: self.data.page + 1
            })
          }
        }else{
          self.setData({
            DataList: [],
            hasMoreData: false
          })
        }
      }
    })
  }
})